#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@File: CrsReportCase.py
@Author: lijk34925 
@Date: 2024/12/12 19:52
@desc: 请补充本模块功能说明
"""
import os

from AmlScripts.amlConfig import amlConfig
from BladeFerm.databases import DB_APP
from DataTest.sqlmontage.SQLMontage import get_sql, get_sql_key, montage_sql_value
from BladeFerm.Lib.Common import logger
from DataTest.tabledata.test_raw import raw_list


"""
反洗钱5.0的数据生成调用函数
基础数据文件查找路径： DataTest/tabledata
逻辑：填写环境名称和表名，指定生成的数据量，点击脚本运行即可
"""


class CreateAmlData:
    def __init__(self, env='mysql_auto', **kwargs):
        self.params = kwargs
        system_odbc_config = amlConfig.get(env).get('databases')
        DB_APP.make_db(**system_odbc_config)
        # 设置环境变量
        cur_file = os.path.abspath(__file__)
        cur_file_path = os.path.split(cur_file)[0]
        project_root_path = cur_file_path.split('web应用')[0] + "\web应用"
        os.chdir(project_root_path)
        logger.info(f'当前工作目录: {os.getcwd()}')
        self.test_data_path = project_root_path + "\\DataTest\\tabledata"
        os.environ['TestDataPath'] = self.test_data_path
        logger.info(f'测试基础数据文件查找路径: {self.test_data_path}')

    """
    生成测试数据
    :return: 
    """
    def make_hsRaw_data(self):
        table_name = self.params.get('table_name')
        id_1 = self.params.get('init_client_id')
        rows = self.params.get('init_client_id')
        batch_number = self.params.get('batch_number')
        table_value = get_sql(table=table_name)
        sql = get_sql_key(table=table_name)
        for _ in range(rows):
            for _ in range(batch_number):
                id_1 = id_1 + 1
                customerid = '6H' + str(id_1)
                sql_list = raw_list()
                sql_list.update({'customerid': customerid})
                values = montage_sql_value(table=table_value, sql_list=sql_list)
                sql += values + ','
            sql = sql.rstrip(',')
            logger.info('sql:' + sql)
            res = DB_APP.get_run_sql_result([sql])
            if not res:
                logger.error(f"数据插入失败：{res}")
                break
        logger.info("数据生成完成：总共插入了 {} 条数据".format(rows * batch_number))


if __name__ == '__main__':
    # AmlScripts/HGConfig.py  中配置的环境名称，
    # 可选值：['mysql', 'oracle', 'oracle_trust', 'BigData', 'smoke', 'dm', 'tdsql_for_mysql', 'pg', 'td_for_pg', 'ob',
    # 'ob_trust', 'mysql_auto', 'dm_auto', 'ob_perfma', 'mysql_perfma', 'oracle_perfma', 'dm_perfma']
    env_name = 'mysql_auto'
    params = {
        'table_name': 'taaml_n_customerinfo_fund',      # 需要插入数据的表名, DataTest/tabledata 必须存在 表名+.json文件和1条数据
        'init_client_id': 1001001000,                   # 初始客户id
        'rows': 1,                                      # 生成多少次数据， 数据总量为  rows * batch_number
        'batch_number': 1                               # 每次生成多少条数据 Oracle必须为1，其他可批量插入，例如500
    }
    TOOL = CreateAmlData(env_name, **params)
    TOOL.make_hsRaw_data()
